// packageJ/anytime_service/my/role_apply_for/enterprise/enterprise.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    can_apply:0,
    
    mobile:"",
    realname:"",
    introduce:"",
    corp_address:"",
    corp_contact:"",
    corp_business_scope:"",
    corp_legal_person:"",
    corp_license_img:"",
    id_card_reverse_img:"",
    id_card_front_img:"",
    corp_image:[],
    corp_image_imgs:[],
    id_card_reverse:[],
    id_card_front:[],
    corp_license:[],

    apply_status:null,//0-待审核、-1 驳回审核
    imgPath: app.globalData.imgPath,
  },
  
  delete(e) {
    let field = e.currentTarget.dataset.field;
    this.setData({
      [field]: []
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {
   await this.checkStauts();
   await this.getData();
  },
  checkStauts(){
    app._getNetWork({
      url:app.getNetAddresss("plugin.be-within-call.frontend.role-apply.detail"),
      data:{role:3},
      success:resdata=>{
        let {result,data} = resdata.data;
        if(result){
            this.setData({can_apply:data.can_apply,apply_status:data.apply_status})
        }
        console.log(data);
      }
    })
  },
  onInput(e) {
    let field = e.currentTarget.dataset.field;
    let value = e.detail;
    this.setData({
      [field]: value
    })
  },
  getData(){
    app._getNetWork({
      url:app.getNetAddresss("plugin.be-within-call.frontend.role-apply.detail"),
      data:{
        identity:3,
        validate_page:1
      },
      success:resdata=>{
        let {result,msg,data} = resdata.data;
        if(result){
          console.log(data);
          this.setData({
            apply_status:data.apply_status,
            can_apply:data.can_apply
          })
        }else{
          app.tips(msg);
        }
      }
    })
  },
  afterRead(event){
    wx.showLoading({
      title: '上传中'
    })
    const {
      file
    } = event.detail;
    let {
      field
    } = event.currentTarget.dataset;
    let urlStr = app.getNetAddresss("upload.uploadPic");
    wx.uploadFile({
      url: urlStr,
      filePath: file.url,
      name: 'file',
      success: (resdata) => {
        app.tips("上传成功")
        let res = JSON.parse(resdata.data);
        if(field!=='corp_image'){
          let arr = []
          arr.push({
            ...file,
            url: res.data.img_url,
            status: "done",
            message: '上传完成'
          });
          this.setData({
            [field]: arr
          });
          if (field == 'corp_license') {
            this.setData({
              corp_license_img: res.data.img
            })
          } else if(field == "id_card_reverse") {
            this.setData({
              id_card_reverse_img: res.data.img
            })
          }else if(field == "id_card_front") {
            this.setData({
              id_card_front_img: res.data.img
            })
          }
        }else{
          let arr = this.data.corp_image;
          arr.push({
            ...file,
            url: res.data.img_url,
            status: "done",
            message: '上传完成'
          });
          this.setData({[field]: arr});
          this.data.corp_image_imgs.push(res.data.img);
          this.setData({corp_image_imgs:this.data.corp_image_imgs});
        }
    
        wx.hideLoading();
      },
      fail(err) {
        app.tips("上传失败")
        console.log(err);
        wx.hideLoading();
      }
    });
  },
  submit() {
    let json = {
      identity:3,
      mobile:this.data.mobile,
      realname:this.data.realname,
      detail_info:{
        introduce:this.data.introduce,
        corp_address:this.data.corp_address,
        corp_contact:this.data.corp_contact,
        corp_business_scope:this.data.corp_business_scope,
        corp_legal_person:this.data.corp_legal_person,
      },
      annex_file:{
        corp_license:this.data.corp_license_img,
        id_card_reverse:this.data.id_card_reverse_img,
        id_card_front:this.data.id_card_front_img,
        corp_image:this.data.corp_image_imgs
      }
    }
    app._postNetWork({
      url:app.getNetAddresss("plugin.be-within-call.frontend.role-apply.index"),
      data:{data:json},
      success:resdata=>{
        let {result,msg} = resdata.data;
        app.tips(msg);
        if(result){
          setTimeout(()=>{
            wx.redirectTo({
              url: '/packageJ/anytime_service/my/index/index',
            })
          },300)
        }
      }
    })
  },
  deletePhoto(e){
    this.data.corp_image_imgs.splice(e.detail.index,1);
    this.data.corp_image.splice(e.detail.index,1);
    this.setData({
      corp_image:this.data.corp_image,
      corp_image_imgs:this.data.corp_image_imgs,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})